package com.c0nflict.server;

import java.sql.DriverManager;
import java.sql.SQLException;

//import javax.naming.Context;
//import javax.naming.InitialContext;
//import javax.naming.NamingException;
import javax.sql.DataSource;

import com.mysql.jdbc.Connection;

public class Database {
	private DataSource ds;
	private Connection dbConnection = null;
	
	public Connection getConnection() throws SQLException { 
		if (ds!=null) return (Connection) ds.getConnection();
		
		if (dbConnection.isClosed()) {
			String url = "jdbc:mysql://localhost:3306/dforum";
			//ds = DataSource
			//dbConnection = 
			dbConnection = (Connection)DriverManager.getConnection(url, "dforum", "d4um");
		}
		return dbConnection; }
	
	public Database() {
		/*try {
			
			   try {
				Context envCtx = (Context) new InitialContext().
				    lookup("java:comp/env");
				
				ds = (DataSource) envCtx.lookup
			       ("jdbc/dforum");
				
				
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				dbConnection = (Connection) ds.getConnection();
			} catch (Exception e) {*/
			//e.printStackTrace();
				try {
				ds = null;
				Class.forName("com.mysql.jdbc.Driver");
				String url = "jdbc:mysql://localhost:3306/dforum";
				dbConnection = (Connection)DriverManager.getConnection(url, "dforum", "d4um");
			
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
